# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import typing
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from config import get_settings


def init_middlewares(app: FastAPI):
    # 跨域中间件
    cors_middleware: typing.Type[any] = CORSMiddleware
    app.add_middleware(
        cors_middleware,
        max_age=3600,
        allow_credentials=True,
        allow_origins=get_settings().CORS_ORIGINS if get_settings().CORS_ORIGINS else ["*"],
        allow_methods=get_settings().CORS_ALLOW_METHODS if get_settings().CORS_ALLOW_METHODS else [
            "GET",
            "POST",
            "PUT",
            "DELETE",
            "OPTIONS"
        ],
        allow_headers=get_settings().CORS_ALLOW_HEADERS if get_settings().CORS_ALLOW_HEADERS else [
            "DNT",
            "Accept",
            "Accept-Encoding",
            "Accept-Language",
            "Cache-Control",
            "Connection",
            "Host",
            "Origin",
            "Content-Type",
            "Sec-Fetch-Mode",
            "Keep-Alive",
            "User-Agent",
            "If-Match",
            "If-None-Match",
            "If-Modified-Since",
            "If-Unmodified-Since",
            "X-Mx-ReqToken",
            "X-Requested-With",
            "Authorization",
            "Terminal",
            "Version",
            "Permits",
            "Secrets",
            "Identity"
        ]
    )
